package com.coffee.concurrent;

/**
 * @Author King.Song
 * @since 2020/7/31 0031
 */
public class CoffeeDisruptorPool {

    private final CoffeeDisruptor[] children;
    private final int size;

    public CoffeeDisruptorPool(int size, String name) {
        this.size = size;
        this.children = new CoffeeDisruptor[size];
        for (int i = 0; i < size; i++) {
            children[i] = new CoffeeDisruptor(1024, name + "-" + i);
        }
    }

    public void start() {
        for (CoffeeDisruptor executor : children) {
            executor.start();
        }
    }

    public CoffeeDisruptor getByBizNo(int no) {
        no = no < 0 ? ~no : no < size ? no + size : no;  // TODO
        return getByIndex(no % size);
    }


    public CoffeeDisruptor getByIndex(int index) {
        if (index < 0 || index >= children.length) {
            return null;
        }
        return children[index];
    }

    public int size() {
        return size;
    }


}
